package dm.repository;

import test.utility.Randomly;
import test.utility.SqlRun;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 * 元数据仓库
 * Hint的元信息
 * 需要增加对特殊hint信息的处理
 */
public final class DmHintsInfo {
    public static ArrayList<String> hintsPara = new ArrayList<String>();

    public static int hintsParaCnt = 0;
    public static String getHintNameSql = "select PARA_NAME from V$HINT_INI_INFO;";

    //初始化dm hints参数项
    public synchronized static void initDmHintsInfo(SqlRun sqlr){
        hintsPara.clear();
        hintsParaCnt = 0;
        ResultSet rs = sqlr.sqlWithRsBegin(getHintNameSql);

        try {
            while(rs.next()){
                hintsPara.add(rs.getString(1));
                hintsParaCnt++;
            }
        } catch (SQLException e) {
            System.out.println("init DmHintsInfo Failed!");
            System.out.println(e.getMessage());
        } finally {
            sqlr.sqlWithRsFinish();
        }
    }

    //返回dm hint
    public static String getDmHintValue(int ind){
        StringBuilder sql = new StringBuilder();
        if(ind < 0){
            sql.append(hintsPara.get(0));
        }else if(ind >= hintsParaCnt){
            sql.append(hintsPara.get(hintsParaCnt - 1));
        }else {
            sql.append(hintsPara.get(ind));
        }

        sql.append("(");
        sql.append(Randomly.fromOptions(0 , 1 , 2 , 3 , 4 , 8 , 16 , 100 , 1000));
        sql.append(")");
        return sql.toString();
    }

    //随机返回dm hints
    public static String getRandom(){
        int ind = (int)Randomly.getNotCachedInteger(0 , hintsParaCnt);
        return getDmHintValue(ind);
    }

}
